<template>
	<view class="mainpadding beijingse">
		<view class="">
			<view class="ffffff boxbai flexbetween shangj" v-for="item in list.data" :key="item.id" @click="tzsoxq(item.id)">
				<image class="sptp" :src="item.image_text" mode=""></image>
				<view class="" style="width: 65%;">
					<view class="titletext xiaohei ">{{item.name}}</view>
					<view class="flexbetween margin_top">
						<view class="strongtext xiaocheng fonweight ">￥
							<text class="bigtext xiaocheng fonweight">{{item.price}}</text>
						</view>
						<view class="flexright">
							<view class="margin_right2" @click.stop="reduceitem(item)">
								<u-icon name="minus-circle" color="#FF9341" size="24" ></u-icon>
							</view>
							<view class="" @click.stop="additem(item)">
								<u-icon name="plus-circle" color="#FF9341" size="24" ></u-icon>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="mainpadding gudingdb">
			<view class="dibox flexbetween">
				<view class="flexleft">
					<view class="dingwei margin_right3" @click="tzgwcxq">
						<image class="heji " src="../static/image/system/hj.png" mode=""></image>
						<view class="huasuz flexcenter">{{allnum}}</view>
					</view>
					<view class="line margin_right3"></view>
					<view class="bigtext xiaohei ">合计
						<text class="bigtext xiaocheng fonweight">￥{{allprice}}</text>
					</view>
				</view>
				<view class="qdbtn flexcenter" @click="buy">确定</view>
			</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				page: 1,
				limit: 12,
				list: {
					data: []
				},
				gwclist:[],
				allnum:0,
				allprice:0,
				user_level_id:""
				
			}
		},
		onLoad(options) {
			this.user_level_id = options.user_level_id
			this.init()
		},
		onShow() {
			uni.$off("changegwcList")
			uni.$on("changegwcList",data=>{
				this.gwclist = data
			})
		},
		watch:{
			gwclist:{
				deep:true,
				handler(val){
					let number = 0
					let price = 0
					this.gwclist.forEach(item=>{
						number = Number(item.number)+Number(number)
						price = Number(item.price)*item.number+Number(price)
					})
					this.allnum = number
					this.allprice = price
				}
			}
		},
		methods: {
			additem(current){
				let flag = false
				this.gwclist.forEach(item=>{
					if(item.id==current.id){
						flag = true
						item.number = Number(item.number) + 1
					}
				})
				if(flag==false){
					this.gwclist.push({
						id:current.id,
						number:1,
						price:current.price
					})
				}
			},
			reduceitem(current){
				let flag = false
				let index = 0
				this.gwclist.forEach((item,index)=>{
					if(item.id==current.id){
						item.number = Number(item.number) - 1
						if(item.number==0){
							flag = true
							index = index
						}
					}
				})
				if(flag){
					this.gwclist.splice(index,1)
				}
			},
			buy(){
				let data = {
					type: "", //detail=商品详情,cart=购物车
					ly:4,//1=普通商品,2=换货,3=复购专区,4=会员购买
					data: [],
					user_level_id:this.user_level_id
				}
				let num = 0
				this.gwclist.forEach(item => {
					data.data.push({
						goods_id: item.id,
						number: item.number,
					})
					num = num + 1
				})
				if (num == 0) {
					httpRequest.toast("请选择商品")
					return
				}
				uni.navigateTo({
					url: '/pages_homepage/querendd?msg=' + encodeURIComponent(JSON.stringify(data))
				})
			},
			init(isPage, page) {
				let _this = this
				httpRequest.request('/api/goods/goodsIndex', 'GET', {
					page: page || 1,
					limit: _this.limit,
					index_rec: 0,
				}).then(res => {
					if (res.code == 1) {
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			// 跳转购物车详情
			tzgwcxq() {
				let data = {
					user_level_id:this.user_level_id,
					data:this.gwclist
				}
				uni.navigateTo({
					url: '/pages_homepage/gouwuchexq?msg='+encodeURIComponent(JSON.stringify(data))
				})
			},
			// 跳转商品详情
			tzsoxq(id) {
				uni.navigateTo({
					url: '/pages_homepage/shangpinxq?ly=4&id='+id
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.qdbtn {
		width: 193rpx;
		height: 75rpx;
		background: linear-gradient(85deg, #FFB67F 0%, #FF9341 100%);
		border-radius: 52rpx 52rpx 52rpx 52rpx;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
	}

	.line {
		width: 2rpx;
		height: 42rpx;
		background-color: #D8D8D8;
	}

	.huasuz {
		position: absolute;
		top: -3rpx;
		right: -13rpx;
		width: 27rpx;
		height: 27rpx;
		background: #FF9341;
		border-radius: 50%;
		font-size: 17rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
	}

	.heji {
		width: 37.5rpx;
		height: 39.58rpx;
	}

	.dibox {
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 14rpx 0rpx rgba(0, 0, 0, 0.05);
		border-radius: 53rpx 53rpx 53rpx 53rpx;
		padding: 13rpx 13rpx 13rpx 41rpx;
	}

	// 商品
	.shangj {
		margin-top: 30rpx;
	}

	.shangj:first-child {
		margin-top: 0rpx;
	}

	.sptp {
		width: 198rpx;
		height: 198rpx;
		border-radius: 11rpx 11rpx 11rpx 11rpx;
	}

	//结束
	.boxbai {
		padding: 16rpx;
		border-radius: 11rpx;
	}
</style>